# import asyncio
# import websockets
#
# async def send_chinese_data(websocket):
#     # 从本地获取中文数据
#     chinese_data = "这是来自服务器的中文数据。"
#     await websocket.send(chinese_data)
#
# async def echo(websocket, path):
#     async for message in websocket:
#         # 从客户端接收消息
#         print("接收到消息:", message)
#
#         # 发送中文数据到客户端
#         await send_chinese_data(websocket)
#
# start_server = websockets.serve(echo, "localhost", 8765)
#
# asyncio.get_event_loop().run_until_complete(start_server)
# asyncio.get_event_loop().run_forever()

import asyncio
import json

import websockets


async def send_data(websocket):
    while True:
        # 在这里编写发送数据的逻辑
        with open(r'11.json', 'r') as file:
            content = file.read()
            json_list = json.loads(content)

            for item in json_list:
                send_json = json.dumps(item)
                print(send_json)
                await websocket.send(send_json)
                await asyncio.sleep(0.1)  # 1秒发送一次数据


async def echo(websocket, path):
    await send_data(websocket)


start_server = websockets.serve(echo, "10.100.12.46", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
